﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Server.API.Application.Command.Breed;
using Server.API.DTOs;

namespace Server.API.Application.Handler.Breed
{
    public class GetBreedOneHandler : IRequestHandler<GetBreedOneCommand, APIResult<BreedDTO>>
    {
        private readonly IRepository<BreedModel> breedway;
        private readonly ILogger<GetBreedOneHandler> logger;
        private readonly IMapper mapper;

        public GetBreedOneHandler(IRepository<BreedModel> breedway, ILogger<GetBreedOneHandler> logger, IMapper mapper)
        {
            this.breedway = breedway;
            this.logger = logger;
            this.mapper = mapper;
        }
        /// <summary>
        /// 养殖信息
        /// </summary>
        /// <param name="request">养殖主键</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>养殖信息</returns>
        public async Task<APIResult<BreedDTO>> Handle(GetBreedOneCommand request, CancellationToken cancellationToken)
        {
            APIResult<BreedDTO> res = new APIResult<BreedDTO>();
            var value = await breedway.GetValue(request.BreedId);
            res.Data = mapper.Map<BreedDTO>(value);
            if (res.Data == null)
            {
                res.Num = StateCode.Fail;
                res.Message = "不存在此种";

            }
            else
            {
                res.Num = StateCode.OK;
                res.Message = "获取成功";

            }
            return res;

        }
    }
}
